package com.logistics.admin;

import cn.easybuild.core.exceptions.ApplicationException;
import cn.easybuild.core.exceptions.DuplicateKeyException;
import cn.easybuild.core.service.StringPojoAppBaseService;
import cn.easybuild.pojo.QueryParam;
import cn.easybuild.pojo.StringPojo;


public abstract class AdminGenericBaseAction<P extends StringPojo, S extends StringPojoAppBaseService<P>> extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6521424104166556254L;
	
	public AdminGenericBaseAction() {
		super(true);
	}
	
	public abstract S getService();
	
	/**
	 * 初始化用于新增操作的Pojo实例
	 *
	 * @return
	 */
	protected abstract P initEntityForAdd();
	
	/**
	 * 显示新增界面
	 * @return
	 */
	public String showAddView(){
		initAddForm();
		
		return SUCCESS;
	}

	/**
	 * 初始化新增界面
	 */
	protected void initAddForm() {
		
	}
	
	/**
	 * 显示编辑界面
	 * 
	 * @return
	 */
	public String showEditView(){
		initEditForm();
		return SUCCESS;
	}

	/**
	 * 初始化编辑界面
	 */
	protected void initEditForm() {
		
	}
	 
	/**
	 * 初始化用于更新操作的Pojo实例
	 *
	 * @return
	 */
	protected P initEnitiyForEdit() {
		return initEntityForAdd();
	}
	
	
	public String save(){
		if (getService() == null) {
			return LIST;
		}
		if (isAddOperation()) {
			
			try {
				
				P pojo = initEntityForAdd(); 
				getService().save(pojo);
				afterSaving(pojo);
				
			} catch (Exception e) {
				return doAddException(e);
			}
			
		}else{
			
			try {
				
				P pojo = initEnitiyForEdit();
				if (pojo == null) {
					return LIST;
				}
				
				pojo.setEntityId(getId());
				getService().update(pojo);
				afterUpdating(pojo);
				
			} catch (Exception e) {
				return doUpdateException(e);
			}
			
		}
		return getSavedResultName();
	}
	
	/**
	 * 保存成功后，默认跳转到查询首页
	 *
	 * @return
	 */
	protected String getSavedResultName() {
		return LIST;
	}
	
	
	/**
	 * 保存成功的后续操作
	 *
	 * @param pojo
	 */	
	protected void afterSaving(P pojo){
		
	}
	
	/**
	 * 更新成功的后续操作
	 *
	 * @param pojo
	 */
	protected void afterUpdating(P pojo) {

	}
	
	/**
	 * 当新增操作出现异常，调用该方法
	 *
	 * @param exception
	 * @return
	 */
	protected String doAddException(Exception exception) {
		if (exception instanceof DuplicateKeyException) {
			addActionError(getDuplicateKeyExceptionString());
			initAddForm();
			return INPUT;
		}
		
		throw new ApplicationException(exception);
	}
	
	/**
	 * 当更新操作出现异常，调用该方法
	 *
	 * @param exception
	 * @return
	 */
	protected String doUpdateException(Exception exception) {
		
		if (exception instanceof DuplicateKeyException) {
			addActionError(getDuplicateKeyExceptionString());
			initEditForm();
			return INPUT;
		}
		
		throw new ApplicationException(exception);
	}
	
	protected String getDuplicateKeyExceptionString() {
		return "数据已存在";
	}

	/**
	 * 删除
	 * @return
	 */
	public String deleteById(){
		if (getService() == null) {
			return LIST;
		}
		
		getService().deleteById(getId());
		
		return LIST;
	}
	
	/**
	 * 查询
	 * @param param
	 * @param call
	 * @return
	 */
	public String executeQuery(QueryParam param , QueryCall call){
		if (param != null) {
			param.setPaging(getPaging());
		}
		
		if (call != null) {
			call.execute();
		}
		
		return SUCCESS;
	}
	
	public interface QueryCall{
		public void execute();
	}	
}
